# 模块名和模块测试APP名称
MODULE_NAME       := 03_char_led
APP_NAME          := $(MODULE_NAME)_app
ARCH              ?= arm
PWD               := $(shell pwd)
#=====================================================
INCDIRS 		  := ./ 			   
SRCDIRS			  := ./

INCLUDE			  := $(patsubst %, -I %, $(INCDIRS))
#=====================================================
# NFS 共享目录
TFTP_SERVER       ?= ~/3tftp
NFS_SERVER        ?= ~/4nfs

TFTP_DIR          ?= $(TFTP_SERVER)/imx6ull
ROOTFS            ?= $(NFS_SERVER)/buildroot
#ROOTFS_MODULE_DIR ?= $(ROOTFS)/lib/modules/4.1.15-gfea85803-dirty
ROOTFS_MODULE_DIR ?= $(ROOTFS)/app
#=====================================================
ifeq ($(KERNELRELEASE),)

# 选择可执行文件运行的平台
ifeq ($(ARCH), arm)
KERNELDIR         ?= ~/5ALPHA/linux-imx-rel_imx_4.1.15_2.1.0_ga
APP_GCC_TOOL      ?= arm-linux-gnueabihf-gcc
else
KERNELDIR         ?= /lib/modules/$(shell uname -r)/build
APP_GCC_TOOL      ?= gcc
endif


# 编译模块和测试程序
modules:
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=$(PWD) modules $(INCLUDE)
	$(APP_GCC_TOOL) $(APP_NAME).c -Wall -o $(APP_NAME) $(INCLUDE)

modules_install:
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install

# 拷贝相关文件到nfs共享目录
install:
	sudo cp $(MODULE_NAME).ko $(ROOTFS_MODULE_DIR)
	sudo cp $(APP_NAME) $(ROOTFS_MODULE_DIR)

uninstall:
	sudo rm $(ROOTFS_MODULE_DIR)/$(MODULE_NAME).ko 
	sudo rm $(ROOTFS_MODULE_DIR)/$(APP_NAME)

.PHONY: clean
clean:
	rm -rf  *.o  *.ko  .*.cmd  *.mod.*  modules.order  Module.symvers   .tmp_versions .cache.mk $(APP_NAME)

help:
	@echo "\033[1;32m================================ Help ================================\033[0m"
	@echo "Ubuntu may need to add sudo:"
	@echo "insmod <module_name>.ko   # Load module"
	@echo "rmmod <module_name>       # Uninstall the module"
	@echo "dmesg -C                  # Clear the kernel print information"
	@echo "lsmod                     # Check the kernel modules that have been inserted"
	@echo "dmesg                     # View information printed by the kernel"
	@echo "file <module_name>.ko     # View \".Ko\" file information"
	@echo "make ARCH=arm             # arm platform"
	@echo "\033[1;32m======================================================================\033[0m"

print:
	@echo "KERNELDIR = $(KERNELDIR)"
	@echo "INCLUDE   = $(INCLUDE)"

else
CONFIG_MODULE_SIG = n
obj-m            += $(MODULE_NAME).o
endif